package com.topscomm.app.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.basic.exception.BusinessException;
import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.cbo.model.TreeModel;
import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.app.pojo.AppPageTabEntity;
import com.topscomm.app.service.IAppPageTabService;
import com.topscomm.app.service.IAppPageService;
import com.topscomm.app.IAppController;
import com.topscomm.app.controller.auto.AppPageItemsControllerAuto;
import com.topscomm.app.pojo.AppPageEntity;
import com.topscomm.app.pojo.AppPageItemsEntity;
import com.topscomm.app.service.impl.AppPageItemsService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

@RestController
@RequestMapping("app/appPageItems")
public class AppPageItemsController extends AppPageItemsControllerAuto implements IAppPageItemsController {
	@Resource(name = "appPageService")
	protected IAppPageService appPageService;
	
	@Resource(name = "appPageTabService")
	protected IAppPageTabService appPageTabService;
	
//	qry_parentid
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();		
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_parentid")) {
			String value = paraMap.get("qry_parentid").toString();
			if (!StringUtil.isEmpty(value) || !value.equals("-1") ||!value.equals("0"))
				sql.appendSingle(" and pagetabid = '{0}'", value);
		}
		if (paraMap.containsKey("qry_key")) {
			String value = paraMap.get("qry_key").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and key like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_title")) {
			String value = paraMap.get("qry_title").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and title like '%{0}%'", value);
		}
		return sql.toString();
	}
	/**
	 * @description:
	 * @param paraMap
	 * @return
	 * @author: wangxinghua
	 * @date: 2020年4月14日下午12:44:54
	 * @modify:
	 */
	@GetMapping(value = "/queryTreeList")
	public ResponseResult<Object> queryTreeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		String sqlWhere = "enabled =1 order by code";
		List<AppPageEntity> listTmpEntity = appPageService.queryByWhere(sqlWhere);
		List<Map<String, Object>> listTmpMap = new ArrayList<Map<String, Object>>();
		Map rootMap = new HashMap<>();
		rootMap.put("code", "");
		rootMap.put("dispenabled","");
		rootMap.put("enabled", "0");
		rootMap.put("id","0");//主目录不对应绑定列表
		rootMap.put("label", "Tab页签");
		rootMap.put("name","Tab页签");
		rootMap.put("pid", "-1");
		rootMap.put("sortcode","0");
		for (AppPageEntity tmpEntity : listTmpEntity) {
			Map tmpMap = new HashMap();
			tmpMap.put("code", ConvertUtil.convertToString(tmpEntity.getValue(AppPageEntity.FieldCode)));
			tmpMap.put("name", tmpEntity.getValue(AppPageEntity.FieldName));
			tmpMap.put("label", tmpEntity.getValue(AppPageEntity.FieldName));
			tmpMap.put("id","0");//一级目录不对应绑定列表
			tmpMap.put("sortcode", "0");
			tmpMap.put("pid", 0);
			tmpMap.put("enabled",tmpEntity.getValue(AppPageEntity.FieldEnabled));
			List<Map> childList = new ArrayList<>();
			listTmpMap.add(tmpMap);
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendSingle(" enabled=1  ");
			sql.appendSingle(" and pageid='{0}'  ", tmpEntity.getId());
			sql.appendSingle(" order by sortcode");
			List<AppPageTabEntity> listTabEntity = appPageTabService.queryByWhere(sql.toString());
			for (AppPageTabEntity tab : listTabEntity) {
				Map tmpMap1 = new HashMap();
				tmpMap1.put("id", ConvertUtil.convertToString(tab.getValue(AppPageTabEntity.FieldId)));
				tmpMap1.put("pid",ConvertUtil.convertToString(tmpEntity.getValue(AppPageEntity.FieldId)));
				tmpMap1.put("code", ConvertUtil.convertToString(tab.getValue(AppPageTabEntity.FieldCode)));
				tmpMap1.put("name", tab.getValue(AppPageTabEntity.FieldName));
				tmpMap1.put("label",tab.getValue(AppPageTabEntity.FieldName));
				tmpMap1.put("enabled",ConvertUtil.convertToString(tab.getValue(AppPageTabEntity.FieldEnabled)));
				tmpMap1.put("sortcode", "0");
				childList.add(tmpMap1);
			}
			tmpMap.put("children", childList);
		}
		rootMap.put("children", listTmpMap);
		Map<String, Object> dataMap = new HashMap<>();
		List<Map> childList = new ArrayList<>();
		childList.add(rootMap);
		dataMap.put("dataList", childList);
		return ResponseResult.ok(dataMap);
	}

}
